package com.hbm.usercenter.base;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.hbm.usercenter.R;

import java.util.LinkedList;


public abstract class BaseActivityCommon extends ActionBarActivity {
    // 放置共有的方法
    // 1.activity完全退出
    // 2.退出提示
    // 3.返回当前的activity

    public static LinkedList<BaseActivityCommon>	mAllActivities	= new LinkedList<BaseActivityCommon>();
    private long							mPreClickTime;
    public BaseActivityCommon						mCurActivity;

    public BaseActivityCommon getCurActivity() {
        return mCurActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        init();
        initData();
        initListener();

    }

    @Override
    protected void onResume() {
        mCurActivity = this;
        super.onResume();
    }

    protected void init() {

    }

    protected abstract void initView();

    public Toolbar initToolbar(String title) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView toolbar_title = (TextView) findViewById(R.id.toolbar_title);
        setSupportActionBar(toolbar);
        toolbar_title.setText(title);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        return toolbar;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    protected void initData() {

    }

    protected void initListener() {

    }

    /**
     * 完成退出
     */
    public void exit() {
        for (BaseActivityCommon activity : mAllActivities) {
            activity.finish();
        }
    }

//    @Override
//    public void onBackPressed() {
//        if (this instanceof MainActivity) {
//
//            if (System.currentTimeMillis() - mPreClickTime > 2000) {
//                Toast.makeText(UIUtils.getContext(), "再按一次，退出谷歌市场", 0).show();
//                mPreClickTime = System.currentTimeMillis();
//                return;
//            } else {
//                exit();
//            }
//        }
//        super.onBackPressed();
//    }

}
